<?php
class Tourism_order_reviews_model extends CI_Model
{
    private $table   = 'tourism_order_reviews';
    private $table_2 = 'tourism_reviews_reply';
    private $table_3 = 'tourism_order';
    private $table_4 = 'tourism_order_product';
    private $table_5 = 'user';

    public function insert($postData)
    {
        $data = array(
            'tourism_order_id' => $postData['tourism_order_id'],
            'order_id'         => $postData['order_id'],
            'goods_attr_id'    => $postData['goods_attr_id'],
            'goods_image'      => $postData['goods_image'],
            'attribute_value'  => $postData['attribute_value'],
            'goods_name'       => $postData['goods_name'],
            'uid'              => $postData['uid'],
            'user_name'        => $postData['user_name'],
            'personal_photo'   => !empty($postData['personal_photo']) ? $postData['personal_photo'] : '',
            'user_content'     => $postData['user_content'],
            'status'           => 1,
            'review_score'     => $postData['review_score'],
            'created_at'       => date('Y-m-d H:i:s'),
            'updated_at'       => '0000-00-00 00:00:00'
        );

        return $this->db->insert($this->table,$data);
    }

    public function insertBatch($postData)
    {
        if (empty($postData)) {
            return false;
        }
        return $this->db->insert_batch($this->table, $postData);
    }

    public function total($goodsAttrId)
    {
        $this->db->select("COUNT(id) AS 'countReviews',COUNT(CASE WHEN `review_score`> 3 THEN '好评' END) AS 'goodReviews',
                        COUNT(CASE WHEN `review_score`= 3 THEN '中评' END) AS 'mediumReviews',
                        COUNT(CASE WHEN `review_score`< 3 THEN '差评' END) AS 'poorReviews'");
        $this->db->from($this->table.' AS tourism_order_reviews');
        $this->db->where('tourism_order_reviews.goods_attr_id', $goodsAttrId);
        $this->db->where('status',2);
        $this->db->limit(1);
        return $this->db->get()->row();
    }

    /**
     * 计算--总数
     * @param $goodsAttrId
     * @param int $reviewScore
     * @return mixed
     */
    public function totalStatus($goodsAttrId,$reviewScore = 0)
    {
        $this->db->where('goods_attr_id', $goodsAttrId);
        $this->db->where('status',2);
        switch($reviewScore){
            case 1:
                $this->db->where('review_score > 3');
                break;
            case 2:
                $this->db->where('review_score = 3');
                break;
            case 3:
                $this->db->where('review_score < 3');
                break;
            default:
                break;
        }
        return $this->db->count_all_results($this->table);
    }

    /**
     * 评价
     * @param $page_num
     * @param $num
     * @param $goodsAttrId
     * @param int $reviewScore
     * @return mixed
     */
    public function page_list($page_num, $num, $goodsAttrId, $reviewScore = 0)
    {
        $this->db->select('tourism_order_reviews.id,tourism_order_reviews.attribute_value,
                 tourism_order_reviews.user_name,tourism_order_reviews.user_content,
                 tourism_order_reviews.review_score,tourism_order_reviews.created_at,
                 tourism_order_product.number,tourism_order_reviews.personal_photo,tourism_reviews_reply.reply_content');
        $this->db->from($this->table . ' AS tourism_order_reviews');
        $this->db->join($this->table_2 . ' AS tourism_reviews_reply', 'tourism_order_reviews.id = tourism_reviews_reply.review_id', 'left');
        $this->db->join($this->table_4 . ' AS tourism_order_product', 'tourism_order_product.order_product_id = tourism_order_reviews.order_product_id');
        switch($reviewScore){
            case 1:
                $this->db->where('review_score > 3');
                break;
            case 2:
                $this->db->where('review_score = 3');
                break;
            case 3:
                $this->db->where('review_score < 3');
                break;
            default:
                break;
        }
        $this->db->where('tourism_order_reviews.goods_attr_id', $goodsAttrId);
        $this->db->where('tourism_order_reviews.status',2);
        $this->db->order_by('created_at', 'desc');
        $this->db->limit($page_num,$num);
        return $this->db->get();
    }

    /**
     * 获取当前评论的产品
     * @param array $params
     * @return mixed
     */
    public function findByGoosAttrId($params=array())
    {
        $this->db->select('tourism_order_reviews.*, tourism_order_product.goods_attr_id, tourism_order_product.goods_image, tourism_order_product.market_price, tourism_order_product.shop_price, tourism_reviews_reply.id AS reply_id, tourism_reviews_reply.reply_user_name, tourism_reviews_reply.reply_content, tourism_reviews_reply.created_at AS reply_time');
        $this->db->from($this->table.' AS tourism_order_reviews');
        $this->db->join($this->table_4 . ' AS tourism_order_product', 'tourism_order_product.order_product_id = tourism_order_reviews.order_product_id');
        $this->db->join($this->table_2 . ' AS tourism_reviews_reply', 'tourism_order_reviews.id = tourism_reviews_reply.review_id', 'left');
        
        $this->db->where('tourism_order_reviews.goods_attr_id', $params['goods_attr_id']);
        if (!empty($params['uid'])) {
            $this->db->where('tourism_order_reviews.uid', $params['uid']);
        }
        return $this->db->get();
    }

    public function reviewTotal($params)
    {
        $this->db->select('*');
        $this->db->from($this->table);
        $this->db->where('uid', $params['uid']);
        $this->db->where('status', $params['status']); //审核通过
        return $this->db->count_all_results();
    }

    /**
     * 查询评论信息
     * @param $params
     * @param $start
     * @param $count
     * @return mixed
     */
    public function queryReview($params, $start, $count)
    {
        $this->db->select('tourism_order_reviews.goods_name, review_score, user_content, tourism_order_reviews.created_at, tourism_order_product.goods_image AS images');
        $this->db->from($this->table);
        $this->db->join($this->table_4, 'tourism_order_product.goods_attr_id = tourism_order_reviews.goods_attr_id');
        $this->db->where('uid', $params['uid']);
        $this->db->where('status', $params['status']); //审核通过
        $this->db->limit($count, $start);
        return $this->db->get();
    }
}